package com.css.fxfzypg.modules.baseData.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.baseData.entity.DisasterScene;
import com.css.fxfzypg.modules.baseData.queryParams.DisasterSceneQueryParams;
import com.css.fxfzypg.modules.baseData.service.DisasterSceneService;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 灾害情景管理子模块
 */
@RestController
@RequestMapping("/fxfzypg/baseData")
@Slf4j
public class DisasterSceneController {

    @Resource
    DisasterSceneService disasterSceneService ;
    @Resource
    private SysAreaService sysAreaService;

    /**
     * 新增DisasterScene
     * @param disasterScene
     * @return
     */
    @PostMapping("/saveDisasterScene")
    public RestResponse saveDisasterScene (@RequestBody DisasterScene disasterScene){
        RestResponse restResponse = null ;
        String provinceId = disasterScene.getProvinceId();
        String cityId = disasterScene.getCityId();
        String countiesId = disasterScene.getCountiesId();
        String address=provinceId+","+cityId+","+countiesId;
        String areaNameByAreaId = sysAreaService.getAreaNameByAreaId(address);
        String[] split = areaNameByAreaId.split(",");
        disasterScene.setDsProvince(split[0]);
        disasterScene.setDsCity(split[1]);
        disasterScene.setDsCounties(split[2]);
        try{
            restResponse = RestResponse.succeed(disasterSceneService.saveDisasterScene(disasterScene));
        }catch (Exception e){
            log.error("新增失败",e);
            restResponse = RestResponse.fail("新增失败!");
        }
        return restResponse;
    }

    /**
     * 全查询  模糊查询  为条件
     * @param request
     * @param queryParams
     * @return
     */
    @GetMapping("/getAllDisasterScene")
    public RestResponse getAllDisasterScene(HttpServletRequest request, DisasterSceneQueryParams queryParams){

        RestResponse restResponse = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            queryParams.setCurPage(curPage);
            queryParams.setPageSize(pageSize);

            Map<String,Object> map = disasterSceneService.getAllDisasterScene(queryParams);
            restResponse = RestResponse.succeed(map);
        }catch (Exception e){
            log.error("查询灾害情景管理数据失败!",e);
            restResponse = RestResponse.fail("查询灾害情景管理数据失败");
        }
        return restResponse;
    }

    /**
     * 根据id查询DisasterScene数据
     * @param id
     * @return
     */
    @GetMapping("/getDisasterScene/{id}")
    public RestResponse<DisasterScene> getDisasterScene(@PathVariable("id") String id) {
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(disasterSceneService.getDisasterSceneById(id));
        }catch (Exception e){
            log.error("获取灾害情景管理数据失败!",e);
            restResponse = RestResponse.fail("获取灾害情景管理数据失败!");
        }
        return restResponse;
    }


    /**
     * 逻辑删除，修改数据标志位
     * @param ids
     * @return
     */
    @PatchMapping("/delDisasterScene/{ids}")
    public RestResponse deleteDisasterScene(@PathVariable("ids") List<String> ids) {
        RestResponse restResponse = null;
        try{
            disasterSceneService.deleteDisasterScenes(ids);
            restResponse = RestResponse.succeed();
        }catch (Exception e){
            log.error("删除灾害情景管理数据失败！",e);
            restResponse = RestResponse.fail("删除灾害情景管理数据失败!");
        }
        return restResponse;
    }

    /**
     * 修改
     * @param disasterScene
     * @return
     */
    @PostMapping("/updateDisasterScene")
    public RestResponse<DisasterScene> updateDisasterScene(@RequestBody DisasterScene disasterScene)  {
        RestResponse restResponse = null;

        String provinceId = disasterScene.getProvinceId();
        String cityId = disasterScene.getCityId();
        String countiesId = disasterScene.getCountiesId();
        String address=provinceId+","+cityId+","+countiesId;
        String areaNameByAreaId = sysAreaService.getAreaNameByAreaId(address);
        String[] split = areaNameByAreaId.split(",");
        disasterScene.setDsProvince(split[0]);
        disasterScene.setDsCity(split[1]);
        disasterScene.setDsCounties(split[2]);

        try{
            restResponse = RestResponse.succeed(disasterSceneService.updateDisasterScene(disasterScene));
        }catch (Exception e){
            log.error("修改失败!",e);
            restResponse = RestResponse.fail("修改失败!");
        }
        return restResponse;
    }

    /**
     * 导出表内数据
     * @param response
     * @throws IOException
     */
//    @PostMapping("/exportExcelDisasterScene")
//    public void exportExcel(HttpServletResponse response ) throws IOException {
//        RestResponse restResponse = null;
//        try{
//            List<DisasterScene> list  = disasterSceneService.getAll();
//            FileUtil.exportExcel(list, "灾害情景管理数据", "sheet", DisasterScene.class, "DisasterScene", response);
//            restResponse = RestResponse.succeed("导出成功");
//        }catch (Exception e){
//            log.error("导出失败",e);
//            restResponse = RestResponse.fail("导出失败");
//        }
//    }

//    /**
//     * 导入数据
//     * @param file
//     * @throws IOException
//     */
//    @PostMapping("/importExcelDisasterScene")
//    public RestResponse importExcel(MultipartFile file) throws IOException {
//        RestResponse restResponse = null;
//        try{
//            disasterSceneService.importExcel(file);
//            restResponse = RestResponse.succeed("导入成功");
//        }catch (Exception e){
//            log.error("导入失败",e);
//
//        }
//        return restResponse ;
//    }
}
